﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace InSys.Documents
{
    public partial class FormReportListDocumentByUser : Form
    {
        private string kindDocument;
        private string userName;
        private AccessUserLoginInfo userInfo;
        private int id_user;
        MapTestLinqDataContext db;

        public FormReportListDocumentByUser()
        {
            InitializeComponent();
            kindDocument = "";
            userName = "";
            id_user = -1;
            userInfo = new AccessUserLoginInfo();
            db = new MapTestLinqDataContext();
        }

        public FormReportListDocumentByUser(int id_user)
            : this()
        {
            this.id_user = id_user;
        }

        private void FormReportListDocumentByUser_Load(object sender, EventArgs e)
        {
            db.ObjectTrackingEnabled = false;
            kindDocument = (from item in db.kindDocument where item.id == Program.gKindDocument select item.kind_document).FirstOrDefault();
            userName = userInfo.GetUserNameById(id_user);
            PersonComboBox.DataSource = from item in db.persons orderby item.name select item;
            PersonComboBox.SelectedValue = id_user;
            //this.reportViewer1.RefreshReport();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.id_user = (int)PersonComboBox.SelectedValue;
            DataTableListDocumentsByPersonTableAdapter.Fill(testlinqDataSet.DataTableListDocumentsByPerson,id_user, dateTimePicker1.Value);
            ReportParameter rpUserName = new ReportParameter("ReportParameterUser", userName);
            ReportParameter rpDate = new ReportParameter("ReportParameterDate", dateTimePicker1.Value.ToString());
            reportViewer1.LocalReport.SetParameters(new ReportParameter[] {rpUserName, rpDate });
            reportViewer1.RefreshReport();
        }

        private void PersonComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            userName = userInfo.GetUserNameById(this.id_user);
        }
    }
}
